<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="用户" prop="user.id"
                :rules="[
                  {required: true, message:'用户不能为空', trigger:['blur','change']}
                 ]">
                <user-select :limit='1' :value="inputForm.user.id" @getValue='(value) => {inputForm.user.id=value}' readonly></user-select>
           </el-form-item>
        </el-col>
		<el-col :span="12">
		    <el-form-item label="所属公司" prop="company.id"
		        :rules="[
		          {required: true, message:'公司不能为空', trigger:'blur'}
		         ]">
		            <SelectTree
		              ref="company"
		              :props="{
		                  value: 'id',             // ID字段名
		                  label: 'name',         // 显示名称
		                  children: 'children'    // 子级字段名
		                }"
		
		              url="/sys/office/treeData?type=2"
		              :value="inputForm.company.id"
		              :clearable="true"
		              :accordion="true"
		              @getValue="(value) => {inputForm.company.id=value}"/>
		   </el-form-item>
		</el-col>
        <el-col :span="12">
            <el-form-item label="所属部门" prop="department.id"
                :rules="[
                  {required: true, message:'所属部门不能为空', trigger:'blur'}
                 ]">
                   <SelectTree
                      ref="department"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'children'    // 子级字段名
                        }"

                      url="/sys/office/treeData?type=2"
                      :value="inputForm.department.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.department.id=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="签约时间" prop="signingtime"
                :rules="[
                  {required: true, message:'签约时间不能为空', trigger:'blur'}
                 ]">
              <!-- <el-input v-model="inputForm.signingtime" placeholder="请填写签约时间"     ></el-input> -->
              <el-date-picker
                style="width: 100%;"
                v-model="inputForm.signingtime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择">
              </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开始时间" prop="begintime"
                :rules="[
                  {required: true, message:'开始时间不能为空', trigger:'blur'}
                 ]">
              <!-- <el-input v-model="inputForm.begintime" placeholder="请填写开始时间"     ></el-input> -->
              <el-date-picker
                style="width: 100%;"
                v-model="inputForm.begintime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择">
              </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="到期时间" prop="endtime"
                :rules="[
                  {required: true, message:'到期时间不能为空', trigger:'blur'}
                 ]">
              <!-- <el-input v-model="inputForm.endtime" placeholder="请填写到期时间"     ></el-input> -->
               <el-date-picker
                style="width: 100%;"
                v-model="inputForm.endtime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择">
              </el-date-picker>
           </el-form-item>
        </el-col>     
        <!-- <el-col :span="12">
            <el-form-item label="人员是否确认" prop="userissure"
                :rules="[
                  {required: true, message:'人员是否确认不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.userissure" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('usersure')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="人员薪资" prop="usersalary"
                :rules="[
                  {required: true, message:'人员薪资不能为空(数字)', trigger:'blur',pattern: /^[0-9]*$/,}
                 ]">
              <el-input v-model="inputForm.usersalary" ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="试用期时间" prop="probationperiod"
                :rules="[
                  {required: true, message:'试用期时间不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.probationperiod"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="试用期薪资" prop="probationsalary"
                :rules="[
                  {required: true, message:'试用期薪资不能为空(数字)', trigger:'blur',pattern: /^[0-9]*$/,}
                 ]">
              <el-input v-model="inputForm.probationsalary"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="人员身份证号" prop="idcard"
                :rules="[
                  {required: true, message:'人员身份证号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.idcard"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="人员手机号" prop="phone"
                :rules="[
                  {required: true, message:'人员手机号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.phone"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="人员岗位名称" prop="postname"
                :rules="[
                  {required: true, message:'人员岗位名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.postname"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="在职状态" prop="jobstatus"
                :rules="[
                  {required: true, message:'在职状态不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.jobstatus" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('employment_status')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
             <el-form-item label="合同有效期" prop="earlywarning"
                :rules="[
                 ]">
                <el-input v-model="inputForm.earlywarning"></el-input>
              </el-form-item>     
        </el-col>   
        <el-col :span="12">
            <el-form-item label="附件" prop="annexe"
                :rules="[
                  {required: true, message:'附件不能为空', trigger:'blur'}
                 ]">
                      <!-- :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"   :on-preview="(file) => {document.location.href = file.url}"-->
                  <el-upload ref="annexe"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/sys/usercontractuser/userContractUser`"
                    :headers="{token: $cookie.get('token')}"                    
                    :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                    :on-success="(response, file, fileList) => {
                       inputForm.annexe = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.annexe = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="annexeArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
              </el-form-item>
          </el-col>       
          <el-col :span="12"   v-if="method==='view'">
              <el-form-item label="签名照片" prop="photo"
                  :rules="[
                    {required: true, message:'签名照片不能为空', trigger:'blur'}
                  ]">
                        <!-- :on-preview="(file, fileList) => {
                          $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                            dangerouslyUseHTMLString: true,
                            showConfirmButton: false,
                            closeOnClickModal: true,
                            customClass: 'showPic'
                          });
                      }" -->
                <el-upload ref="photo"
                v-if="visible"
                list-type="picture-card"
                      :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/sys/usercontractuser/userContractUser`"
                      :headers="{token: $cookie.get('token')}"
                      :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                      :on-success="(response, file, fileList) => {
                        inputForm.photo = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                      }"
                      :on-remove="(file, fileList) => {
                        $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                          $message.success(data.msg)
                        })
                        inputForm.photo = fileList.map(item => item.url).join('|')
                      }"
                      :before-remove="(file, fileList) => {
                        return $confirm(`确定移除 ${file.name}？`)
                      }"
                      multiple
                      :limit="5"
                      :on-exceed="(files, fileList) =>{
                        $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                      }"
                      :file-list="photoArra">
                      <i class="el-icon-plus"></i>
                    </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import UserSelect from '@/components/userSelect'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        photoArra: [],
        annexeArra: [],
        inputForm: {
          id: '',
          user: {
            id: ''
          },
          department: {
            id: ''
          },
          company: {
            id: ''
          },
          photo: '',
          annexe: '',
          begintime: '',
          endtime: '',
          userissure: '',
          usersalary: '',
          probationperiod: '',
          probationsalary: '',
          idcard: '',
          phone: '',
          postname: '',
          jobstatus: '',
          signingtime: '',
          earlywarning: ''
        }
      }
    },
    components: {
      UserSelect,
      SelectTree
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建系统用户劳动合同`
        } else if (method === 'edit') {
          this.title = '修改系统用户劳动合同'
        } else if (method === 'view') {
          this.title = '查看系统用户劳动合同'
        }
        this.photoArra = []
        this.annexeArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/sys/usercontractuser/userContractUser/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.userContractUser)
              this.inputForm.photo.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.photoArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.annexe.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.annexeArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/sys/usercontractuser/userContractUser/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>